package com.atguigu.springcloud.service.Impl;

import com.atguigu.springcloud.dao.OrderDao;
import com.atguigu.springcloud.entity.Order;
import com.atguigu.springcloud.service.AccountService;
import com.atguigu.springcloud.service.OrderService;
import com.atguigu.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private AccountService accountService;
    @Resource
    private StorageService storageService;

    @Override
    @GlobalTransactional(name = "mrz-create-order",rollbackFor = Exception.class)
    public void create(Order order) {
        // 新建订单
        log.info("------>开始新建订单");
        orderDao.create(order);
        // 扣减库存
        log.info("------>订单微服务开始调用库存，做扣减Count");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("------>订单微服务开始调用库存，做扣减END");

        // 扣减账户
        log.info("------>订单服务开始调用账户扣钱");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("------>订单微服务开始调用账户扣钱结束");

        // 修改订单状态 从零到1，1代表已经完成
        log.info("------>订单微服务修改订单状态");
        orderDao.update(order.getUserId(),0);
        log.info("------>修改订单状态完成");

        log.info("------>整个下订单结束了");
    }
}
